package yxy.apple.http.response;

import org.apache.http.HttpResponse;

import yxy.apple.http.HTTPException;
import yxy.apple.util.Base64Utility;
import yxy.apple.util.ByteUtility;
import yxy.apple.util.http.HTTPProtocol;

public class ImageResponse extends HTTPResponse<byte[]> {

	private String mime = null;

	@Override
	protected String bodyToString(byte[] body) {
		return ByteUtility.toHexString(body);
	}

	@Override
	protected byte[] decodeBody(byte[] body) throws HTTPException {
		return body;
	}

	public String base64Encode() {
		byte[] data = this.body();
		if (data != null) {
			return Base64Utility.encodeToString(data);
		} else {
			return null;
		}
	}

	public void respond(HttpResponse response) throws HTTPException {
		super.respond(response);
		this.mime = response.getFirstHeader(HTTPProtocol.HeaderKey.ContentType).getValue();
	}

	public String mime() {
		return this.mime;
	}
}
